﻿@{
    string searchTerm = Request.QueryParam("searchTerm");
    string priceFrom = Request.QueryParam("priceFrom");
    string priceTo = Request.QueryParam("priceTo");
    int? categoryID = Model.Selected.Category == null ? null : JDM.Int32(Model.Selected.Category.EntityID);
    int? sellerID = Model.Selected.Seller == null ? null : JDM.Int32(Model.Selected.Seller.EntityID);
    int? supplierID = Model.Selected.Supplier == null ? null : JDM.Int32(Model.Selected.Supplier.EntityID);
    int? itemID = Model.Selected.Item == null ? null : JDM.Int32(Model.Selected.Item.EntityID);
    var attributeIDs = (Model.Selected.Attributes as IEnumerable<dynamic>).Select<dynamic, Int32>(x => JDM.Int32(x.EntityID));
    var act = Loc.Act("Index").Use("view",
        "sortBy", "sortDesc", "searchTerm", "priceFrom", "priceTo",
        "categoryID", "sellerID", "supplierID", "attributeIDs",
        "itemID").Localized();
}
<div class="filter sales-item">
    @if(!String.IsNullOrWhiteSpace(searchTerm) || 
        !String.IsNullOrWhiteSpace(priceFrom) || 
        !String.IsNullOrWhiteSpace(priceTo) ||
        sellerID.HasValue || 
        supplierID.HasValue || 
        categoryID.HasValue || 
        itemID.HasValue || 
        attributeIDs.Any()) 
    {
        <dl class="selected">
            <dt>@T(Res.Name("Selected"))</dt>
            @if(!String.IsNullOrWhiteSpace(searchTerm))
            {
                <dd>
                    <ul class="bar inlinebar">
                        <li><a href="@Url.Action(act.Copy().Rst("searchTerm"))">x</a></li>
                    </ul>
                    <label>@searchTerm</label>
                </dd>
            }
            @if(!String.IsNullOrWhiteSpace(priceFrom) || !String.IsNullOrWhiteSpace(priceTo))
            {
                <dd>
                    <ul class="bar inlinebar">
                        <li><a href="@Url.Action(act.Copy().Rst("priceFrom", "priceTo"))">x</a></li>
                    </ul>
                    <label>@String.Format("{0} - {1}", priceFrom, priceTo)</label>
                </dd>
            }
            @if(sellerID.HasValue)
            {
                <dd>
                    <ul class="bar inlinebar">
                        <li><a href="@Url.Action(act.Copy().Rst("sellerID"))">x</a></li>
                    </ul>
                    <label>@JDM.String(Model.Selected.Seller.DisplayName)</label>
                </dd>
            }
            @if(supplierID.HasValue)
            {
                var t_supplier = (Model.Selected.Supplier.Translations as IEnumerable<dynamic>)
                   .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                       JDM.Int32(Svc.Localization.Language.EntityID));
                <dd>
                    <ul class="bar inlinebar">
                        <li><a href="@Url.Action(act.Copy().Rst("supplierID"))">x</a></li>
                    </ul>
                    <label>@(t_supplier != null ? JDM.String(t_supplier.Name) : JDM.String(Model.Selected.Supplier.EntityID))</label>
                </dd>
            }
            @if(itemID.HasValue)
            {
                var t_item = (Model.Selected.Item.Translations as IEnumerable<dynamic>)
                   .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                       JDM.Int32(Svc.Localization.Language.EntityID));
                <dd>
                    <ul class="bar inlinebar">
                        <li><a href="@Url.Action(act.Copy().Rst("itemID"))">x</a></li>
                    </ul>
                    <label>@(t_item != null ? JDM.String(t_item.Name) : JDM.String(Model.Selected.Supplier.EntityID))</label>
                </dd>
            }
            @if(categoryID.HasValue)
            {
                var t_cat = (Model.Selected.Category.Translations as IEnumerable<dynamic>)
                   .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                       JDM.Int32(Svc.Localization.Language.EntityID));
                <dd>
                    <ul class="bar inlinebar">
                        <li>
                        @if(Model.Selected.Category.Parent == null)
                        {
                            <a href="@Url.Action(act.Copy().Rst("categoryID", "attributeIDs"))">x</a>
                        }
                        else
                        {
                            <a href="@Url.Action(act.Copy().Rst("attributeIDs").Set(new
                            {
                                categoryID = JDM.String(Model.Selected.Category.Parent.EntityID)
                            }))">x</a>
                        }
                        </li>
                    </ul>
                    <label>@(t_cat != null ? JDM.String(t_cat.Name) : JDM.String(Model.Selected.Category.EntityID))</label>
                </dd>
            }
            @if(attributeIDs.Any())
            {
                var tmp = new List<dynamic>();
                foreach(var i in Model.Selected.Attributes)
                {
                    var t_attr = (i.Translations as IEnumerable<dynamic>)
                            .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                                JDM.Int32(Svc.Localization.Language.EntityID));
                    tmp.Add(new
                    {
                        Entity = i,
                        NameValue = t_attr != null ? String.Format("{0}: {1}", JDM.String(t_attr.Name), JDM.String(t_attr.Value)) : JDM.String(i.EntityID)
                    });
                }
                foreach(var i in tmp)
                {
                    <dd>
                        <ul class="bar inlinebar">
                            <li><a href="@Url.Action(act.Copy().Set(new
                            {
                                attributeIDs = attributeIDs.Except(new int[]
                                {
                                    JDM.Int32(i.Entity.EntityID)
                                }).Select(x => x.ToString()).Merge(",")
                            }))">x</a></li>
                        </ul>
                        <label>@i.NameValue</label>
                    </dd>
                }
            }
        </dl>
    }
    <form class="common-form" action="@Url.Action(Loc.Act("Index").Localized())" method="get">
        <input type="hidden" name="view" value="@Request.QueryParam("view")" />
        <input type="hidden" name="sortBy" value="@Request.QueryParam("sortBy")" />
        <input type="hidden" name="sortDesc" value="@Request.QueryParam("sortDesc")" />
        <input type="hidden" name="searchTerm" value="@searchTerm" />
        <input type="hidden" name="categoryID" value="@categoryID" />
        <input type="hidden" name="attributeIDs" value="@attributeIDs.Select(x => x.ToString()).Merge(",")" />
        <input type="hidden" name="sellerID" value="@sellerID" />
        <input type="hidden" name="supplierID" value="@supplierID" />
        <input type="hidden" name="itemID" value="@itemID" />
        <table>
            <tr>
                <td>@T(Res.Name("PriceFrom"))</td>
                <td><input type="text" name="priceFrom" value="@(String.IsNullOrWhiteSpace(priceFrom) ? Svc.Price(Decimal.Zero) : priceFrom)" /></td>
            </tr>
            <tr>
                <td>@T(Res.Name("PriceTo"))</td>
                <td><input type="text" name="priceTo" value="@(String.IsNullOrWhiteSpace(priceTo) ? Svc.Price(Decimal.Zero) : priceTo)" /></td>
            </tr>
        </table>
        <ul class="bar toolbar">
            <li><button class="button" type="submit">@T(Res.In("Common").Of("Act").Name("Apply"))</button></li>
        </ul>
    </form>
    <form class="search-form" action="@Url.Action(Loc.Act("Index").Localized())" method="get">
        <input type="hidden" name="view" value="@Request.QueryParam("view")" />
        <input type="hidden" name="sortBy" value="@Request.QueryParam("sortBy")" />
        <input type="hidden" name="sortDesc" value="@Request.QueryParam("sortDesc")" />
        <input type="hidden" name="priceFrom" value="@priceFrom" />
        <input type="hidden" name="priceTo" value="@priceTo" />
        <input type="hidden" name="categoryID" value="@categoryID" />
        <input type="hidden" name="attributeIDs" value="@attributeIDs.Select(x => x.ToString()).Merge(",")" />
        <input type="hidden" name="sellerID" value="@sellerID" />
        <input type="hidden" name="supplierID" value="@supplierID" />
        <input type="hidden" name="itemID" value="@itemID" />
        <input type="text" name="searchTerm" value="@searchTerm" maxlength="100" />
        <input type="submit" value="@T(Res.In("Common").Of("Act").Name("Search"))" />
        <span class="hint">@T(Res.Name("Search"))</span>
    </form>
    @if((Model.Filter.Categories as IEnumerable<dynamic>).Any())
    {
        var tmp = new List<dynamic>();
        foreach(var i in Model.Filter.Categories)
        {
            var t = (i.Translations as IEnumerable<dynamic>)
                   .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                       JDM.Int32(Svc.Localization.Language.EntityID));
            tmp.Add(new
            {
                Entity = i,
                Name = t != null ? JDM.String(t.Name) : JDM.String(i.EntityID)
            });
        }
        <dl>
            <dt>@T(Res.Name("Categories"))</dt>
            @foreach(var i in tmp.OrderBy(x => x.Name))
            {
                <dd><a href="@Url.Action(act.Copy().Rst("attributeIDs").Set(new
                {
                    categoryID = JDM.String(i.Entity.EntityID)
                }))">@i.Name</a></dd>
            }
        </dl>
    }
    @if((Model.Filter.Attributes as IEnumerable<dynamic>).Any())
    {
        var tmp = new List<dynamic>();
        foreach(var i in (Model.Filter.Attributes as IEnumerable<dynamic>)
            .Where(x => JDM.HasFlags(x.Flags, "AllowedForSearch"))
            .OrderBy(x => JDM.Int32(x.DisplayOrder)))
        {
            var t_attr = (i.Translations as IEnumerable<dynamic>)
                   .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                       JDM.Int32(Svc.Localization.Language.EntityID));
            tmp.Add(new
            {
                Entity = i,
                Name = t_attr != null ? JDM.String(t_attr.Name) : JDM.String(i.EntityID),
                Value = t_attr != null ? JDM.String(t_attr.Value) : JDM.String(i.EntityID),
            });
        }
        foreach(var g in tmp.GroupBy(x => x.Name))
        {
            <dl>
                <dt>@T(Res.Name("Attributes"), g.Key)</dt>
                @foreach(var i in g)
                {
                    <dd>
                    @if(attributeIDs.Any(x => x == JDM.Int32(i.Entity.EntityID)))
                    {
                        <span>@i.Value</span>
                    }
                    else
                    {
                        <a href="@Url.Action(act.Copy().Set(new
                        {
                            attributeIDs = attributeIDs.Concat(new int[]
                            {
                                JDM.Int32(i.Entity.EntityID)
                            }).Select(x => x.ToString()).Merge(",")
                        }))">@i.Value</a>
                    }
                    </dd>
                }
            </dl>
        }
    }
</div>
